package com.ruoyi.train.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.train.mapper.TrainClassMapper;
import com.ruoyi.train.domain.TrainClass;
import com.ruoyi.train.service.ITrainClassService;

/**
 * 培训班级Service业务层处理
 * 
 * @author wusihao
 * @date 2023-03-20
 */
@Service
public class TrainClassServiceImpl extends ServiceImpl<TrainClassMapper, TrainClass> implements ITrainClassService
{
    @Autowired
    private TrainClassMapper trainClassMapper;

    /**
     * 查询培训班级
     * 
     * @param id 培训班级主键
     * @return 培训班级
     */
    @Override
    public TrainClass selectTrainClassById(Long id)
    {
        return trainClassMapper.selectTrainClassById(id);
    }

    /**
     * 查询培训班级列表
     * 
     * @param trainClass 培训班级
     * @return 培训班级
     */
    @Override
    public List<TrainClass> selectTrainClassList(TrainClass trainClass)
    {
        List<TrainClass> trainClasses = trainClassMapper.selectTrainClassList(trainClass);
        List<TrainClass> ans = new ArrayList<>();
        for (TrainClass trainClass1 : trainClasses)
            if (trainClass1.getIsDeleted() == 0) ans.add(trainClass1);
        return ans;
    }

    /**
     * 新增培训班级
     * 
     * @param trainClass 培训班级
     * @return 结果
     */
    @Override
    public int insertTrainClass(TrainClass trainClass)
    {
        trainClass.setCreateId(SecurityUtils.getUserId());
        trainClass.setCreateTime(DateUtils.getNowDate());
        trainClass.setTrainingInstitutionId(String.valueOf(SecurityUtils.getUserId()));
        return trainClassMapper.insertTrainClass(trainClass);
    }

    /**
     * 修改培训班级
     * 
     * @param trainClass 培训班级
     * @return 结果
     */
    @Override
    public int updateTrainClass(TrainClass trainClass)
    {
        trainClass.setUdpateId(SecurityUtils.getUserId());
        trainClass.setUpdateTime(DateUtils.getNowDate());
        return trainClassMapper.updateTrainClass(trainClass);
    }

    /**
     * 批量删除培训班级
     * 
     * @param ids 需要删除的培训班级主键
     * @return 结果
     */
    @Override
    public int deleteTrainClassByIds(Long[] ids)
    {
        for (int i = 0; i < ids.length; i ++ ) {
            Long id = ids[i];
            TrainClass trainClass = trainClassMapper.selectById(id);
            trainClass.setUdpateId(SecurityUtils.getUserId());
            trainClassMapper.updateTrainClass(trainClass);
        }
        return trainClassMapper.deleteTrainClassByIds(ids);
    }

    @Override
    public List<TrainClass> selectTrainClass(Long trainId) {
        QueryWrapper<TrainClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("train_plan_id", trainId);
        List<TrainClass> trainClasses = trainClassMapper.selectList(queryWrapper);
        List<TrainClass> ans = new ArrayList<>();
        for (TrainClass trainClass1 : trainClasses)
            if (trainClass1.getIsDeleted() == 0) ans.add(trainClass1);
        return ans;
    }

    @Override
    public int trainEnd(Long id) {
        TrainClass trainClass = trainClassMapper.selectTrainClassById(id);
        trainClass.setIsEnd(1);
        trainClass.setUdpateId(SecurityUtils.getUserId());
        return trainClassMapper.updateTrainClass(trainClass);
    }

}
